/**
 * 
 */
package edu.hawaii.its381b.client;

import com.google.gwt.canvas.client.Canvas;
//import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.safehtml.shared.SafeUri;
import com.google.gwt.user.client.ui.Image;
/**
 * @author Matt David
 *
 */
public class Arrows {
	private ImageElement ArrowUpImage;
	private ImageElement ArrowDownImage;
	private ImageElement ArrowLeftImage;
	private ImageElement ArrowRightImage;
	private Canvas canvas;
	
	private int CanvasBorder; 
	private int CanvasWidth;
	private int CanvasHeight;
	
	private int arrowUpX;
	private int arrowDownX;
	private int arrowLeftX;
	private int arrowRightX;
	private int arrowUpY;
	private int arrowDownY;
	private int arrowLeftY;
	private int arrowRightY;
	private int arrowLeftRightWidth = 50;
	private int arrowLeftRightHeight = 100;
	private int arrowUpDownWidth = 100;
	private int arrowUpDownHeight = 50;

	
	
	public Arrows(int canvasWidthAmount, int canvasHeightAmount, int borderAmount, Canvas canvas){
		this.canvas = canvas;
		CanvasWidth = canvasWidthAmount;
		CanvasHeight = canvasHeightAmount;
		CanvasBorder = borderAmount;

		SafeUri ArrowUpURI = HermitCrabResources.INSTANCE.ArrowUp().getSafeUri();
		this.ArrowUpImage = ImageElement.as((new Image(ArrowUpURI)).getElement());

		SafeUri ArrowDownURI = HermitCrabResources.INSTANCE.ArrowDown().getSafeUri();
		this.ArrowDownImage = ImageElement.as((new Image(ArrowDownURI)).getElement());

		SafeUri ArrowLeftURI = HermitCrabResources.INSTANCE.ArrowLeft().getSafeUri();
		this.ArrowLeftImage = ImageElement.as((new Image(ArrowLeftURI)).getElement());

		SafeUri ArrowRightURI = HermitCrabResources.INSTANCE.ArrowRight().getSafeUri();
		this.ArrowRightImage = ImageElement.as((new Image(ArrowRightURI)).getElement());
		
		//Resolve the placement of the top left corner of each Arrow image
		arrowUpX = canvasWidthAmount/2-50;
		arrowUpY = 0;
		arrowDownX = canvasWidthAmount/2-50;
		arrowDownY = canvasHeightAmount-50;
		arrowLeftX = 0;
		arrowLeftY = canvasHeightAmount/2-50;
		arrowRightX = canvasWidthAmount-50;
		arrowRightY = canvasHeightAmount/2-50;
		}
	
	//Draw arrows in on the border
	public void draw(){
		canvas.getContext2d().drawImage(ArrowUpImage, arrowUpX, arrowUpY, arrowUpDownWidth, arrowUpDownHeight);
		canvas.getContext2d().drawImage(ArrowDownImage, arrowDownX, arrowDownY, arrowUpDownWidth, arrowUpDownHeight);
		canvas.getContext2d().drawImage(ArrowLeftImage, arrowLeftX, arrowLeftY, arrowLeftRightWidth, arrowLeftRightHeight);
		canvas.getContext2d().drawImage(ArrowRightImage, arrowRightX, arrowRightY, arrowLeftRightWidth, arrowLeftRightHeight);
		}
	
	//Detect if an arrow was touched
	public String CheckArrowPressed(int xPressed, int yPressed){
		if (xPressed>= arrowUpX && xPressed<= arrowUpX+arrowUpDownWidth &&
			yPressed>= arrowUpY && yPressed<= arrowUpY+arrowUpDownHeight) {
			return "Up";
		}
		
		if (xPressed>= arrowDownX && xPressed<= arrowDownX+arrowUpDownWidth && 
				yPressed>= arrowDownY && yPressed<= arrowDownY+arrowUpDownHeight) {
				return "Down";
			}
		
		if (xPressed>= arrowLeftX && xPressed<= arrowLeftX+arrowLeftRightWidth && 
				yPressed>= arrowLeftY && yPressed<= arrowLeftY+arrowLeftRightHeight) {
				return "Left";
			}
		
		if (xPressed>= arrowRightX && xPressed<= arrowRightX+arrowLeftRightWidth && 
				yPressed>= arrowRightY && yPressed<= arrowRightY+arrowLeftRightHeight) {
				return "Right";
			}
		
		return "Nothing";
	}
	
}
